package com.mathassistant.server;

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.mathassistant.client.service.MyUserService;
import com.mathassistant.server.domain.UserAccount;
import com.mathassistant.shared.UserDTO;

@SuppressWarnings("serial")
public class MyUserServiceImpl extends RemoteServiceServlet implements
		MyUserService {

	@Override
	public UserDTO getUserLogin() {
		// TODO Auto-generated method stub
		PersistenceManager pm = PMF.get().getPersistenceManager();
		UserAccount user = null;
		try {
			user = UserAccount.getDefaultUser();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			pm.close();
		}
		return UserAccount.toDTO(user);
	}

	@Override
	public Boolean createUserAccount(UserDTO iuser) {
		// TODO Auto-generated method stub
		boolean result = false;
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			//kiem tra ton tai email
			if(!checkEmailExist(iuser.getEmail())){
				//lay user hien tai dang login
				UserAccount user = new UserAccount(iuser.getEmail(), iuser.getName(),iuser.getPassword(), iuser.isAdmin());
				pm.makePersistent(user);
				result = true;
			} 
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally {
			pm.close();
		}
		return result;
	}

	@Override
	public UserDTO getUser(String email) {
		// TODO Auto-generated method stub
		PersistenceManager pm = PMF.get().getPersistenceManager();
	    
	    UserDTO result = null;
	    try {
	    	Query q = pm.newQuery(UserAccount.class, "emailAddress == :email");
	    	q.setUnique(true);
	    	UserAccount kq = (UserAccount) q.execute(email);
	    	if(kq != null){
	    		result = new UserDTO(kq.getKey(),kq.getName(),kq.getEmail(),kq.isAdmin());
	    	}
	    } catch (Exception e) {
	        e.printStackTrace();
	    } finally {
	        pm.close();
	    }
	    return result;
	}

	@Override
	public Boolean checkEmailExist(String email) {
		// TODO Auto-generated method stub
		PersistenceManager pm = PMF.get().getPersistenceManager();
	    Boolean result = false;
	    try {
	    	Query q = pm.newQuery(UserAccount.class, "emailAddress == :email");
	    	q.setUnique(true);
	    	UserAccount kq = (UserAccount) q.execute(email);
	    	if(kq != null){
	    		result = true;
	    	}
	    } catch (Exception e) {
	        e.printStackTrace();
	    } finally {
	        pm.close();
	    }
	    return result;
	}
	
	@SuppressWarnings("unchecked")
	public boolean checkExistAcc(){
		boolean result = false;
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Query q = pm.newQuery(UserAccount.class);
			List<UserAccount> kq = (List<UserAccount>) q.execute();
			if(kq != null && kq.size() > 0){
				result = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			pm.close();
		}
		return result;
	}
	@SuppressWarnings("unchecked")
	public boolean checkExistAcc(PersistenceManager pm){
		boolean result = false;
		try {
			Query q = pm.newQuery(UserAccount.class);
			List<UserAccount> kq = (List<UserAccount>) q.execute();
			if(kq != null && kq.size() > 0){
				result = true;
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			pm.close();
		}
		return result;
	}
	
	public void DefaultAdmin(){
		UserDTO iuser = new UserDTO("admin", "admin", "admin@gmail.com", true);
		PersistenceManager pm1 = PMF.get().getPersistenceManager();
		try {
			//lay user hien tai dang login
			UserAccount user = new UserAccount(iuser.getEmail(), iuser.getName(),iuser.getPassword(), iuser.isAdmin());
			pm1.makePersistent(user);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally {
			pm1.close();
		}
	}
	public void DefaultAdmin(PersistenceManager pm){
		UserDTO iuser = new UserDTO("admin", "admin", "admin@gmail.com", true);
		try {
			//lay user hien tai dang login
			UserAccount user = new UserAccount(iuser.getEmail(), iuser.getName(),iuser.getPassword(), iuser.isAdmin());
			pm.makePersistent(user);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}

	@Override
	public UserDTO Login(UserDTO user) {
		// TODO Auto-generated method stub
		UserDTO result = null;
		if(user.getName().equals("admin") && user.getPassword().equals("admin")){
			result = new UserDTO();
			result = user;
		}
//		if(checkExistAcc()){
//			PersistenceManager pm = PMF.get().getPersistenceManager();
//			Query q = pm.newQuery(UserAccount.class, "ten == :name " + "&& pass == :password");
//			q.setUnique(true);
//			UserAccount userAcc = (UserAccount) q.execute(user.getName(), user.getPassword());
//			if(userAcc != null){
//				result = userAcc.toDTO(userAcc);
//			}
//		} else{
//			DefaultAdmin();
//		}
		return result;
	}

	@Override
	public UserDTO Login(String name, String passAcc) {
		// TODO Auto-generated method stub
//		boolean result = false;
//		if(checkExistAcc()){
//			PersistenceManager pm = PMF.get().getPersistenceManager();
//			Query q = pm.newQuery(UserAccount.class, "ten == :name " + "&& pass == :password");
//			q.setUnique(true);
//			UserAccount userAcc = (UserAccount) q.execute(name, passAcc);
//			if(userAcc != null){
//				result = true;
//			}
//		} else{
//			DefaultAdmin();
//		}
		return null;
	}

	@Override
	public void updateUserAccount(UserDTO user) {
		// TODO Auto-generated method stub
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			UserAccount acc = pm.getObjectById(UserAccount.class, user.getId());
			if(acc != null){
				acc.setEmail(user.getEmail());
				acc.setPassword(user.getPassword());
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			pm.close();
		}
	}

	@Override
	public void deleteUserAccount(UserDTO user) {
		// TODO Auto-generated method stub
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			UserAccount acc = pm.getObjectById(UserAccount.class, user.getId());
			if(acc != null){
				pm.deletePersistent(pm.getObjectById(UserAccount.class, user.getId()));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			pm.close();
		}
	}

	@Override
	public void deleteUserAccount(String id) {
		// TODO Auto-generated method stub
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			UserAccount acc = pm.getObjectById(UserAccount.class, id);
			if(acc != null){
				pm.deletePersistent(pm.getObjectById(UserAccount.class, id));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			pm.close();
		}
	}

}
